
package com.workcell.workdata.xthings.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.workcell.workdata.xthings.constant.HardwareStateEnum;
import com.workcell.workdata.xthings.entity.BaseHeatExchangers;
import com.workcell.workdata.xthings.entity.CodeChainnum;
import com.workcell.workdata.xthings.entity.CodeHeatExchangers;
import com.workcell.workdata.xthings.mapper.CodeHeatExchangersMapper;
import com.workcell.workdata.xthings.service.BaseHeatExchangersService;
import com.workcell.workdata.xthings.service.CodeHeatExchangersService;
import com.workcell.workdata.xthings.service.factory.CodeBaseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 码库换热机信息表
 *
 * @author pigx code generator
 * @date 2023-04-19 11:39:52
 */
@Service
public class CodeHeatExchangersServiceImpl extends ServiceImpl<CodeHeatExchangersMapper, CodeHeatExchangers> implements CodeHeatExchangersService, CodeBaseService {
	@Resource
	private BaseHeatExchangersService baseHeatExchangersService;

	/**
	 * 保存二维码基本快照信息
	 * @param codeChainnum
	 * @return
	 */
	@Override
	public Boolean saveCodeBaseInfo(CodeChainnum codeChainnum) {
		// 查询硬件信息
		LambdaQueryWrapper<BaseHeatExchangers> baseHeatExchangersCondition = new QueryWrapper<BaseHeatExchangers>().lambda()
				.eq(BaseHeatExchangers::getId,codeChainnum.getBaseId());
		BaseHeatExchangers baseHeatExchangers = baseHeatExchangersService.getOne(baseHeatExchangersCondition);
		if (ObjectUtil.isEmpty(baseHeatExchangers)){
			return false;
		}
		// 添加二维码快照信息
		CodeHeatExchangers saveBean = new CodeHeatExchangers();
		BeanUtil.copyProperties(baseHeatExchangers,saveBean, "createBy","createTime","updateBy","updateTime");
		saveBean.setCodeId(codeChainnum.getCodeId());
		saveBean.setChainNum(codeChainnum.getChainNum());
		saveBean.setDeviceNo(codeChainnum.getDeviceNo());
		return this.save(saveBean);
	}

	/**
	 * 获取硬件清单
	 * @return
	 */
	@Override
	public List<JSONObject> getHardwareList() {
		LambdaQueryWrapper<BaseHeatExchangers> lambdaQueryWrapper = Wrappers.lambdaQuery(BaseHeatExchangers.class);
		lambdaQueryWrapper.eq(BaseHeatExchangers::getState, HardwareStateEnum.INUSE.getCode());
		return baseHeatExchangersService.list(lambdaQueryWrapper).stream().map(i -> {
			JSONObject resultBean = new JSONObject();
			resultBean.put("baseId",i.getId());
			resultBean.put("manufacturer",i.getManufacturer());
			resultBean.put("hardwareName",i.getName());
			return resultBean;
		}).collect(Collectors.toList());
	}

	/**
	 * 查询硬件基本信息
	 * @param baseIds
	 * @return
	 */
	@Override
	public Object getHardwareBase(String baseIds) {
		List<String> baseList = Arrays.asList(baseIds.split(","));
		LambdaQueryWrapper<BaseHeatExchangers> baseHeatExchangersCondition = new QueryWrapper<BaseHeatExchangers>().lambda()
				.in(CollectionUtil.isNotEmpty(baseList),BaseHeatExchangers::getId,baseList);
		List<BaseHeatExchangers> baseHeatExchangersList = baseHeatExchangersService.list(baseHeatExchangersCondition);
		return baseHeatExchangersList;
	}

	/**
	 * 查询硬件快照信息
	 * @param codeId
	 * @return
	 */
	@Override
	public Object getHardwareInfo(String codeId) {
		return this.getById(codeId);
	}

    @Override
    public Object getHardwareBaseOpen(String deviceNo) {
		LambdaQueryWrapper<CodeHeatExchangers> lambdaQueryWrapper = Wrappers.lambdaQuery(CodeHeatExchangers.class);
		lambdaQueryWrapper.eq(CodeHeatExchangers::getDeviceNo,deviceNo);
		CodeHeatExchangers codeHeatExchangers = this.getOne(lambdaQueryWrapper);
		return codeHeatExchangers;
    }
}
